package org.madgecko.api.service.impl;

import org.apache.commons.lang.StringUtils;
import org.madgecko.api.dao.iface.IMessageDao;
import org.madgecko.api.model.Message;
import org.madgecko.api.model.enumeration.MessageCategory;
import org.madgecko.api.service.iface.IMessageService;
import org.madgecko.basic.kit.BasicKit;
import org.madgecko.basic.model.Pager;
import org.madgecko.basic.model.SystemContext;
import org.madgecko.web.PropertyHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;


/**
 * Author       : MadFrog
 * Date         : 2018/11/10 18:01
 * Description  : 消息类的服务接口的实现
 */
@Service
public class MessageService implements IMessageService {

    @Autowired
    private IMessageDao messageDao;

    @Override
    public Message add(Message message) {
        return messageDao.add(message);
    }

    @Override
    public void delete(Integer id) {
        Message news = messageDao.load(id);
        messageDao.delete(id);
        // 1、删除文章content里包含的图片、文件
        String content = news.getContent();
        String webAttachPath = PropertyHolder.getProperty("config.webAttachPath");
        String contextPath = PropertyHolder.getProperty("config.contextPath");
        String prefix = "/" + contextPath + "/resources";
        int i=0;
        while (i>=0){
            i = content.indexOf(prefix + "/upload/images", i+1);
            // 图片的扩展名可能是3个字符，也可能是4个字符
            if(i>0){
                String imagePath3 = content.substring(i, i+61);
                String imagePath3Abs = webAttachPath + StringUtils.substringAfter(imagePath3, prefix);
                String imagePath4 = content.substring(i, i+62);
                String imagePath4Abs = webAttachPath + StringUtils.substringAfter(imagePath4, prefix);
                tryToDeleteFile(imagePath3Abs);
                tryToDeleteFile(imagePath4Abs);
            }
        }
        int j=0;
        while (j>=0){
            j = content.indexOf(prefix + "/upload/files", j+1);
            // 文件的扩展名可能是2个字符，也可能是3个字符，也可能是4个字符
            if(j>0){
                String filePath2 = content.substring(j, j+59);
                String filePath2Abs = webAttachPath + StringUtils.substringAfter(filePath2, prefix);
                String filePath3 = content.substring(j, j+60);
                String filePath3Abs = webAttachPath + StringUtils.substringAfter(filePath3, prefix);
                String filePath4 = content.substring(j, j+61);
                String filePath4Abs = webAttachPath + StringUtils.substringAfter(filePath4, prefix);
                tryToDeleteFile(filePath2Abs);
                tryToDeleteFile(filePath3Abs);
                tryToDeleteFile(filePath4Abs);
            }
        }
        // 2、删除预览图片
        String preview = news.getPreview();
        if(!BasicKit.isEmpty(preview)){
            String previewAbsPath = webAttachPath + StringUtils.substringAfter(news.getPreview(), prefix);
            tryToDeleteFile(previewAbsPath);
        }
    }

    @Override
    public Message update(Message message) {
        return messageDao.update(message);
    }

    @Override
    public Message load(Integer id) {
        return messageDao.load(id);
    }

    @Override
    public List<Message> listByCategory(MessageCategory messageCategory) {
        return messageDao.listByCategory(messageCategory);
    }

    @Override
    public Pager<Message> find(MessageCategory messageCategory) {
        return messageDao.find(messageCategory);
    }

    private void tryToDeleteFile(String absolutePath){
        File file = new File(absolutePath);
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                System.out.println("删除单个文件" + absolutePath + "成功！");
            } else {
                System.out.println("删除单个文件" + absolutePath + "失败！");
            }
        } else {
            System.out.println("删除单个文件失败：" + absolutePath + "不存在！");
        }
    }
}